package net.elive.peer.http;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import net.elive.peer.p2p.P2PAP;
import net.elive.tools.CharMap;
import net.elive.utils.LogUtils;

import org.apache.commons.logging.Log;

/**
 * http request请求
 * @author Administrator
 *
 */
public class HttpRequest implements Runnable{
	Log  log = LogUtils.getLog(HttpRequest.class);
	/**
	 * socket 套接字
	 */
	private Socket socket = null;
	/**
	 * 主线程
	 */
	private Thread mainThread;
	/**
	 * 数据输入流
	 */
	private DataInputStream dis;
	/**
	 * 数据输出流
	 */
	private DataOutputStream dos;
	/**
	 * 输入流
	 */
	private InputStream is = null;
	/**
	 * 输出流
     */
	private OutputStream os  =null;
	/**
	 * 播放形态
	 */
	private boolean  playMode= false;
	/**
	 * 构造方法
	 * @param socket
	 */
	public HttpRequest(Socket socket){
		this.socket = socket;
	}
	@Override
	public void run() {
		/**获取输入流**/
		try {
			is = socket.getInputStream();
		} catch (IOException e) {
			log.error(">>>>>>>>>>>>>获取输入流失败!!!!");
			e.printStackTrace();
		}
		try {
			os =socket.getOutputStream();
		} catch (IOException e) {
			log.error(">>>>>>>>>>>>>获取输出流失败!!!!");
			e.printStackTrace();
		}
		dis = new DataInputStream(is);		//初始化数据输入流
		dos = new DataOutputStream(os);		//初始化数据输出流
		String inputStr ="";				//输入字符串
		String outputStr="";				//输入字符串
		A: while(true){
			long offset=0;					//偏移量
			boolean haveRange = false;
			try {
				inputStr = dis.readLine();
				log.info(">>>>>输入流字符串信息:"+inputStr);
			} catch (IOException e) {
				log.error(">>>>读取输入流失败,break");
				break;
			}
			/**
			 * 如何输入流字符串为空 程序break
			 */
			if(inputStr == null){
				break;
			}
			while(true){
				try {
					outputStr =dis.readLine();
					log.info(">>>>>输入流字符串信息:"+outputStr);
				} catch (IOException e) {
					break A;
				}
				if(outputStr != null){
					if("".equals(outputStr)){
						break;
					}
				}
				/**
				 * 常用播放器Http请求特性字符串
				 */
				for(String s:P2PAP.playerFeatureStr){
					if(outputStr.startsWith(s)){
						playMode =true;
					}
				}
				if(outputStr.length()>=13){
					String s = outputStr.substring(0,5);
					if(s.equals("Range")){
						int index2 = outputStr.indexOf("=")+1;
						int index3 = outputStr.indexOf('-');
						String offsets = outputStr.substring(index2,index3);
						offset = Long.parseLong(offsets);
	
					}
				}else{
					break;
				}
				
				int n =0;
				for(int j=0;j<inputStr.length();j++){
					String s = inputStr.substring(j,j+1);
					if(s.equals(".")){
						n++;
					}
				}
				if(n<2){
					break;
				}
				
				int index1 = inputStr.indexOf(".")+1;
				int index2 = inputStr.indexOf(".",index1);
				int index3 = inputStr.indexOf("/")+1;
				log.info(">>>>>>>>>>>>>>>>"+inputStr);
				String mds =null;
				if(inputStr.length()>index3+32){
					for(int j=0;j<mds.length();j++){
						String s = mds.substring(j,j+1);
						if(!CharMap.charTable2.containsKey(s)){
							break A;
						}
					}
				}
			}
		}
	}

}
